//
//  EditSystemInfoViewController.swift
//  ezyform
//
//  Created by Moennig Q on 13/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import MBProgressHUD
class EditInstallationTypeViewController: BaseViewController {
    let customView = EditInstallationTypeView()
    var model: PVDDetailModel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.model = JobDetailModelHelper.shared.jobDetailModel
        self.initUI()
        self.initData()
    }
    func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.editInstallationDetails())
        self.customView.installationTypeView.additionalTipBtnClosure = {[weak self] in
            guard let strongSelf = self else { return }
            let vc = AddtionalInfomationTipVC()
            vc.hidesBottomBarWhenPushed = true
            strongSelf.navigationController?.pushViewController(vc, animated: false)
        }
        self.customView.saveBtnClosure = {[weak self] in
            guard let strongSelf = self else { return  }
            strongSelf.saveSystemInfo()
        }
    }
    func initData(){
        guard let model = self.model else { return }
        let viewModel = InstallationTypeViewModel()
        viewModel.pvd_id = JobDetailModelHelper.shared.jobDetailModel.id
        if let type = InstallationTypeValue(rawValue: model.system_detail.installation_type) {
            viewModel.installation_type = type
        }
        viewModel.additional_upgrade_system_details = model.system_detail.additional_upgrade_system_details
        self.customView.setData(viewModel)
    }
    func saveSystemInfo(){
        let vm = EditJobInfoService()
        vm.setBlockAfterNetwork { [weak self] (result, _) in
            guard let strongSelf = self else {return}
            ModelViewUtil.shared.hideUpdatingView()
            strongSelf.navigationController?.popViewController(animated: true)
        } errorBlock: { (code, error) in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        
        ModelViewUtil.shared.showLoadingView(R.string.localizable.updating())
        vm.updateInstallationType(self.customView.installationTypeView.viewModel)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
}
